
class Subject{
    constructor() {
        this.state = 0;
        this.observers = [];
    }
    getState() {
        return this.state;
    }
    setState(state) {
        this.state = state;
        this.observers.forEach((obs) => {
            obs.update(state);
        })
    }
    subscribe(observer) {
        this.observers.push(observer);
    }
}

class Observer{
    constructor(name) {
        this.name = name;
    }
    subscribe(subject) {
        subject.subscribe(this);
    }
    update(msg) {
        console.log(`${this.name} ${msg}`);
    }
}

function main() {
    let s = new Subject();
    let o1 = new Observer("o1");
    let o2 = new Observer("o2");
    o1.subscribe(s);
    o2.subscribe(s);
    s.setState(4);
}

main();
